$(document).ready(function () {
    $('#audio_btn').on({
        click: function (e) {

            var media = $("#media")[0];
            var dom = $("#audio_btn");
            if (dom.hasClass('off')) {
                dom.removeClass('off');
                $("#yinfu").addClass("rotate");
                media.play();
            } else {
                dom.addClass('off');
                $("#yinfu").removeClass("rotate");
                media.pause();
            }
        }
    });
});
var buchouNum =0;
var g_Interval = 1;
var g_Timer;
var running = false;
var level = 3;
var levelMap = {
    0: 1,
    1: 1,
    2: 2,
    3: 3

};
var levelNameMap = {
    0: '特',
    1: '一',
    2: '二',
    3: '三'
};

var winMap = {
    0: ['李永华'],
    1: [],
    2: [],
    3: []
};
var persons = all_persons.concat();
function updateTitle() {
    var res = "";
    if (level < 4 && level > 0) {
        res = res + "" + levelNameMap[level] + "等奖结果为：";
    } else if (level == 0) {
        res = "特等奖的结果为:";
        //alert("管家婆，我爱你，别凶我啦，么么哒");
    }
    $('h2').html(res);
    level > 1 && $('#btn').val("抽取" + levelNameMap[level - 1] + "等奖");
    level == 1 && $('#btn').val("抽取特等奖");
    level == 0 && $('#btn').val("抽奖结束");
};
updateTitle();
function beginRndNum(trigger) {
    if (level < 0) {
        alert("抽奖已经结束");
        return;
    }
    if (running) {
        updateRndNum(true);
        running = false;
        clearTimeout(g_Timer);
        $(trigger).val("开始");
        $('#ResultNum').css('color', 'red');
        updateTitle();
        level--;
        if(level>=0){
            $(".buchou-result").html("");
            $("#buchou").removeClass("hidden");
        }
        if (level < 0) {
            $('.special').removeClass('hidden');
            $('.end').removeClass('hidden');
        }

    }else {
        buchouNum = 0;
        running = true;
        $("#buchou").addClass("hidden");
        $('.buchou-result').addClass("hidden");
        $('#ResultNum').css('color', 'rgb(211,168,9)');
        $(trigger).val("停止");
        $('h2').html("正在抽取的是" + levelNameMap[level] + "等奖");
        beginTimer();

    }

}
function buchou(){
    var count = levelMap[level+1];
    if(buchouNum>=count){
        alert(levelNameMap[level+1]+"等奖最多补抽"+count+"个");
        return;
    }
    var res = "";
    var num = getRadNum();
    var name = persons[num - 1];
    res += (name + "&nbsp;");
    (persons.splice(num - 1, 1));
    var dom = $('.buchou-result');
    dom.removeClass("hidden");
    dom.html(dom.html()+res);
    buchouNum++;
}

function getRadNum() {
    var g_PersonCount = persons.length;//参加抽奖人数
    var num = Math.floor(Math.random() * g_PersonCount + 1);
    return num;
}
function updateRndNum(win) {
    var res = "";
    var count = levelMap[level];
    if (win) {
        var confirmWinArr = winMap[level];
        var confirmCount = confirmWinArr.length;
        count = count - confirmCount;
        for (var j = 0; j < confirmCount; j++) {
            res += (confirmWinArr[j] + "&nbsp;");
        }
    }
    for (var i = 0; i < count; i++) {
        num = getRadNum();
        var name = persons[num - 1];
        res += (name + "&nbsp;");
        win && (persons.splice(num - 1, 1));

    }

    $('#ResultNum').html(res);
}

function beginTimer() {
    g_Timer = setTimeout(beat, g_Interval);
}

function beat() {
    g_Timer = setTimeout(beat, g_Interval);
    updateRndNum();

}

